import { Channel } from "./Channel";
import ChannelTools from "./ChannelTools";
import TTChannel from "../channel/TTChannel";
import QQChannel from "../channel/QQChannel";
import ChannelData from "../data/ChannelData";
import WebChannel from "../channel/WebChannel";
import ChannelBase from "../base/ChannelBase";
import OPPOChannel from "../channel/OPPOChannel";
import VIVOChannel from "../channel/VIVOChannel";
import AppleChannel from "../channel/AppleChannel";
import BaiduChannel from "../channel/BaiduChannel";
import WeiXinChannel from "../channel/WeiXinChannel";
import AndroidChannel from "../channel/AndroidChannel";
import AdsSimulator from "../ads/AdsSimulator";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChannelManager extends ChannelTools {

    /**
     * 单例模式
     */
    private static instance: ChannelManager;
    public static GetInstance(): ChannelManager {
        return this.instance;
    }

    private ads: AdsSimulator;
    private channel_obj: ChannelBase;

    /**
     * 初始化渠道管理器
     */
    protected Init() {
        ChannelManager.instance = this;
        this.JudgeChannel();
        switch (ChannelData.channel) {
            case Channel.ANDROID://安卓渠道
                this.channel_obj = new AndroidChannel("", [], [], [], [], [], []);
                break;
            case Channel.APPLE://苹果渠道
                this.channel_obj = new AppleChannel("", [], [], [], [], [], []);
                break;
            case Channel.BAIDU://百度小游戏渠道
                this.channel_obj = new BaiduChannel("", [], [], [], [], [], []);
                break;
            case Channel.OPPO://OPPO小游戏渠道
                this.channel_obj = new OPPOChannel("", [], [], [], [], [], []);
                break;
            case Channel.QQGAME://QQ小游戏渠道
                this.channel_obj = new QQChannel("1110668784",
                    ["dec5be307f94112faff2f5bd9b78b56f"],
                    ["f62f69eb2ddcf5ddd3ca0ddaceb5de29"],
                    ["f62f69eb2ddcf5ddd3ca0ddaceb5de29"],
                    [], [], []);
                break;
            case Channel.TOUTIAO://头条小游戏渠道
                this.channel_obj = new TTChannel("", [], [], [], [], [], []);
                break;
            case Channel.VIVO://VIVO小游戏渠道
                this.channel_obj = new VIVOChannel("", [], [], [], [], [], []);
                break;
            case Channel.WEIXIN://微信小游戏渠道
                this.channel_obj = new WeiXinChannel("", [], [], [], [], [], []);
                break;
            default://默认网页端
                this.channel_obj = new WebChannel("", [], [], [], [], [], []);
                break;
        }

        let ads = this.node.getChildByName("AdsSimulator");
        if (ads) {
            this.ads = ads.getComponent(AdsSimulator);
            this.ads.initAds();
        }
    }

    /**渠道判断，OPPO VIVO 百度以及安卓平台渠道，需要手动设置 */
    private JudgeChannel() {
        if (typeof tt != "undefined") {
            console.log("Channel:头条小游戏");
            ChannelData.channel = Channel.TOUTIAO;
        } else if (typeof qq != "undefined") {
            console.log("Channel:QQ小游戏");
            ChannelData.channel = Channel.QQGAME;
        } else if (typeof wx != "undefined") {
            console.log("Channel:微信小游戏");
            ChannelData.channel = Channel.WEIXIN;
        } else if (typeof hbs != "undefined") {
            console.log("Channel:华为小游戏");
            ChannelData.channel = Channel.HUAWEI;
        } else if (typeof qg != "undefined") {
            console.log("Channel:OPPO/VIVO小游戏");
        } else if (typeof swan != "undefined") {
            console.log("Channel:百度/华为小游戏");
        }
    }

    /**
     * Toast提示
     * @param content 提示内容，单行显示，建议不要过长
     */
    public ShowToast(content: string) {
        this.showToast(content);
    }

    /**
     * 显示提示框
     * @param data 提示框数据
     * @param ->title string 提框框标题(选填)，默认为“提 示”
     * @param ->content string 提框框标题(必填)，提示的内容
     * @param ->sure Function 点击确认的回调(选填)
     * @param ->cancel Function 点击取消的回调(选填)
     * @param ->sure_label 确认按键上的文字(选填)，默认为“确 定”
     * @param ->cancel_label string 取消按键上的文字(选填)，默认为“取 消”
     * @param ->ad_icon boolean 是否有广告图标(选填)，默认没有
     */
    public ShowDialog(data: any) {
        this.showDialog(data);
    }

    //#region 广告相关
    /**
     * 展示虚拟广告
     * @param on_successed 展示成功回调（获得奖励）
     * @param on_failed 展示失败回调
     */
    public ShowTestAd(on_successed: Function, on_failed: Function) {
        if (this.ads) {
            this.ads.ShowAds(on_successed, on_failed, 5);
        }
    }
    /**
     * 展示视频广告
     * @param on_successed 展示成功回调（获得奖励）
     * @param on_failed 展示失败回调
     */
    public ShowVideo(on_successed: Function, on_failed: Function) {
        if (ChannelData.channel == Channel.WEB) {
            if (ChannelData.is_simulator_ad) {
                this.ads.ShowAds(on_successed, on_failed, ChannelData.simulator_ad_timer);
            } else {
                if (on_successed) on_successed();
            }
        } else if (this.channel_obj && this.channel_obj.is_has_ad) {
            this.channel_obj.ShowVideo(on_successed, on_failed);
        } else {
            if (ChannelData.is_simulator_ad) {
                this.ads.ShowAds(on_successed, on_failed, ChannelData.simulator_ad_timer);
            } else {
                this.ShowToast("暂时没有视频了！");
                if (on_failed) on_failed();
            }
        }
    }

    /**
     * 展示插屏广告
     * @param on_successed 展示成功回调
     * @param on_failed 展示失败回调
     */
    public ShowPage(on_successed: Function, on_failed: Function) {
        if (this.channel_obj) {
            this.channel_obj.ShowPage(on_successed, on_failed);
        }
    }

    /**
     * 展示横幅广告
     * @param on_successed 展示成功回调
     * @param on_failed 展示失败/关闭回调
     */
    public ShowBanner(on_successed: Function, on_failed: Function) {
        if (this.channel_obj) {
            this.channel_obj.ShowBanner(on_successed, on_failed);
        }
    }

    /**
     * 关闭横幅广告
     */
    public HideBanner() {
        if (this.channel_obj) {
            this.channel_obj.HideBox();
        }
    }

    public GetBannerHeight(): number {
        this.channel_obj.banner_height;
        return 0;
    }

    /**
     * 展示盒子广告
     * @param on_successed 展示成功回调
     * @param on_failed 展示失败回调
     */
    public ShowBox(on_successed: Function, on_failed: Function) {
        if (this.channel_obj) {
            this.channel_obj.ShowBox(on_successed, on_failed);
        }
    }

    /**
     * 关闭盒子广告
     */
    public HideBox() {
        if (this.channel_obj) {
            this.channel_obj.HideBox();
        }
    }

    /**
     * 展示积木广告
     * @param on_successed 展示成功回调
     * @param on_failed 展示失败回调
     */
    public ShowJuggle(on_successed: Function, on_failed: Function) {
        if (this.channel_obj) {
            this.channel_obj.ShowJuggle(on_successed, on_failed);
        }
    }

    /**
     * 关闭积木广告
     */
    public HideJuggle() {
        if (this.channel_obj) {
            this.channel_obj.HideJuggle();
        }
    }

    /**
     * 展示原生广告
     * @param on_successed 展示成功回调
     * @param on_failed 展示失败回调
     */
    public ShowNative(on_successed: Function, on_failed: Function) {
        if (this.channel_obj) {
            this.channel_obj.ShowNative(on_successed, on_failed);
        }
    }

    /**
     * 关闭原生广告
     */
    public HideNative() {
        if (this.channel_obj) {
            this.channel_obj.HideNative();
        }
    }
    //#endregion 广告相关

    //#region 分享相关
    /**
     * 分享游戏
     */
    public ShareGame() {
        if (this.channel_obj) {
            this.channel_obj.ShareGame();
        }
    }

    /**
     * 开始录制视频
     */
    public startRecord() {
        if (this.channel_obj) {
            this.channel_obj.StartRecord();
        }
    }

    /**
     * 结束录制视频
     */
    public stopRecord(onStop: Function) {
        if (this.channel_obj) {
            this.channel_obj.StopRecord(onStop);
        }
    }

    /**
     * 分享录制视频
     */
    public shareRecord(on_successed: Function, on_failed: Function) {
        if (this.channel_obj && ChannelData.record_path != null) {
            this.channel_obj.ShareRecord(on_successed, on_failed);
        } else {
            this.ShowToast("暂无可分享视频！");
        }
    }
    //#endregion 分享相关

    //#region 设备相关
    public GetSystemInfo() {
        if (this.channel_obj) {
            return this.channel_obj.GetSystemInfo();
        }
        return undefined;
    }

    /*
     * 短震动
     */
    public VibrateShort() {
        if (ChannelData.vibrate && this.channel_obj) {
            this.channel_obj.VibrateShort();
        }
    }

    /*
     * 长震动
     */
    public VibrateLong() {
        if (ChannelData.vibrate && this.channel_obj) {
            this.channel_obj.VibrateLong();
        }
    }
    //#endregion 设备相关

    //#region 开放功能
    /**
     * 打开另一个小程序
     * @param target 目标小程序appid
     * @param path 打开的页面路径
     * @param extraData 需要传递给目标小程序的数据
     * @param ON_SUCCESS 成功回调,返回参数res
     * @param ON_FAILED 失败回调,返回参数res
     * @param is_developer envVersion 的合法值是否为 develop(开发版)
     */
    public NavigateToMiniProgram(target: string, path: string, extraData: object, ON_SUCCESS: Function, ON_FAILED: Function, is_developer = false) {
        if (this.channel_obj) {
            this.channel_obj.NavigateToMiniProgram(target, path, extraData, ON_SUCCESS, ON_FAILED, is_developer);
        }
    }
    //#endregion 开放功能

    //#region 开关相关
    /**
     * 是否包含广告
     */
    public GetIsHasAd(): boolean {
        return this.channel_obj.is_has_ad;
    }
    /**
     * 是否包含录屏
     */
    public GetIsHasRecorder(): boolean {
        return this.channel_obj.is_has_recorder;
    }
    /**
     * 是否包含分享
     */
    public GetIsHasShare(): boolean {
        return this.channel_obj.is_has_share;
    }
    /**
     * 是否包含退出
     */
    public GetIsHasExit(): boolean {
        return this.channel_obj.is_has_exit;
    }
    /**
     * 是否包含插屏广告
     */
    public GetIsHasPage(): boolean {
        return this.channel_obj.is_has_ad && this.channel_obj.is_has_page;
    }
    /**
     * 是否包含横幅广告
     */
    public GetIsHasBanner(): boolean {
        return this.channel_obj.is_has_ad && this.channel_obj.is_has_banner;
    }
    /**
     * 是否包含盒子广告
     */
    public GetIsHasBox(): boolean {
        return this.channel_obj.is_has_ad && this.channel_obj.is_has_box;
    }
    /**
     * 是否包含积木广告
     */
    public GetIsHasJuggle(): boolean {
        return this.channel_obj.is_has_ad && this.channel_obj.is_has_jm;
    }
    //#endregion 开关相关
}
